﻿using HbmTools.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinHBM.Core.Bluetooth;

namespace HbmTools
{
    public partial class FormMain : Form
    {

        private BluetoothManager bluetoothManager;
        public static Utils.TextBoxBindData textBoxBindData = new Utils.TextBoxBindData();
        private bool found = false;

        public FormMain()
        {
            InitializeComponent();
            init();
        }

        public void init() {
            //Log 输出
            txtLog.DataBindings.Add("Text", textBoxBindData, "TheValue", false, DataSourceUpdateMode.Never);
            //蓝牙管理类
            bluetoothManager = new BluetoothManager();
            //检测/连接/guanbi点击事件
            bt_search.Click += searchAdressFunc;
            bt_connect_open.Click += conectOpenFunc;
            bt_connect_close.Click += conectCloseFunc;
            bt_send.Click +=sendCMDFunc;
            //监听回调
            bluetoothManager.FoundDevice += BluetoothManager_FoundDevice;
            bluetoothManager.DiscoveryCompleted += BluetoothManager_DiscoveryCompleted;
            bluetoothManager.ConnectCompleted += BluetoothManager_ConnectCompleted;
            bluetoothManager.EndConnectCompleted += BluetoothManager_EndConnectCompleted;

        }

        private void sendCMDFunc(object sender, EventArgs e)
        {
            SendCMDManager.INSTANCE().sendCMD(func_comboBox.SelectedItem.ToString(),textBoxBindData);
        }

        private void conectCloseFunc(object sender, EventArgs e)
        {
                bluetoothManager.CloseConnect();
        }

        private void conectOpenFunc(object sender, EventArgs e)
        {
            if (comboBox_adresses.SelectedItem.ToString().Contains(":"))
            {
                bluetoothManager.Connect(comboBox_adresses.SelectedItem.ToString());
            }
            else
            {
                MessageBox.Show(MessageInfo.Error_Bluth_Adress);
            }
        }

        public void searchAdressFunc(object sender, EventArgs e) {

            removeAllComBoxItems(comboBox_adresses);
            comboBox_adresses.Items.Add("请检测");

            if (bluetoothManager != null)  {
               
                bluetoothManager.StartDiscovery();
                textBoxBindData.TheValue = MessageInfo.StartSearch;
            }
        }


        public void removeAllComBoxItems(ComboBox combox)
        {
            for(int i=0; i<combox.Items.Count;i++)
            combox.Items.RemoveAt(i);
        }

        private void BluetoothManager_DiscoveryCompleted()
        {
            if (!found)
            {
                UtilLog.inputLog(textBoxBindData, MessageInfo.StopSearch+":未找到魔法棒设备");
            }
            else
            {
                UtilLog.inputLog(textBoxBindData, MessageInfo.StopSearch);
                comboBox_adresses.SelectedIndex = comboBox_adresses. Items.Count-1;
            }
          
        }

        private void BluetoothManager_FoundDevice(BluetoothManager manager, InTheHand.Net.Sockets.BluetoothDeviceInfo device)
        {
            if (device.DeviceName.Equals("BOLUTEK"))
            {
                found = true;
                JObject data = new JObject();
                //把mac地址每2位按:分割
                string address = device.DeviceAddress.ToString();
                string[] str = new string[address.Length];
                string temp = "";
                for (int i = 0; i < address.Length / 2; i++)
                {
                    str[i] = address.Substring(i * 2, 2);
                    temp = temp + str[i] + ":";
                }
                data["address"] = temp.Substring(0, temp.Length - 1);

                comboBox_adresses.Items.Add(temp.Substring(0, temp.Length - 1));
                UtilLog.inputLog(textBoxBindData, address);
            }
        }

        private void BluetoothManager_EndConnectCompleted()
        {
            UtilLog.inputLog(textBoxBindData, MessageInfo.EndConnectSuccess);
        }

        private void BluetoothManager_ConnectCompleted()
        {
            UtilLog.inputLog(textBoxBindData, MessageInfo.ConnectSuccess);
        }

    }



}
